package com.ruoyi.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.domain.common.UserHolder;
import com.ruoyi.domain.entity.Comment;
import com.ruoyi.domain.vo.CommentPageVo;
import com.ruoyi.domain.vo.CommentVo;
import com.ruoyi.exception.BaseException;
import com.ruoyi.mapper.CommentMapper;
import com.ruoyi.mapper.ForumPostMapper;
import com.ruoyi.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ForumPostMapper forumPostMapper;

    @Override
    public void addComment(Comment comment) {
        if (comment.getPostId() == null || forumPostMapper.selectById(comment.getPostId()) == null){
            throw new BaseException("帖子不存在");
        }
        Integer userId = UserHolder.getUserId();
        comment.setId(null);
        comment.setLikes(0);
        comment.setUserId(userId);
        comment.setCreateTime(LocalDateTime.now());
        save(comment);
    }


    public CommentPageVo getCommentPageVo(Integer postId, Integer pageNo, Integer pageSize){
        //获取用户在该帖子下点赞过的评论列表
        Integer userId = UserHolder.getUserId();
        List<Integer> likeIds;
        if (userId == null){
            likeIds = Collections.emptyList();
        }else{
            likeIds = commentMapper.getLikeIds(userId, postId);
        }
        // 获取一级评论列表
        int begin = (pageNo - 1) * pageSize;
        List<CommentVo> voList = commentMapper.getCommentList(postId, begin, pageSize);
        voList.forEach(vo->{
            ArrayList<CommentVo> replyList = new ArrayList<>();
            getReplyList(postId,vo.getId(),replyList);
            vo.setReply(replyList);
        });
        return new CommentPageVo(likeIds,voList);
    }

    public void getReplyList(Integer postId, Integer commentId, List<CommentVo> list){
        List<CommentVo> replyList = commentMapper.selectReplyList(postId,commentId);
        if(replyList != null && !replyList.isEmpty()){
            list.addAll(replyList);
            //如果回复列表不为空，则继续寻找该评论的回复评论
            replyList.forEach(reply->{
                getReplyList(postId,reply.getId(),list);
            });
        }
    }

    @Transactional
    @Override
    public void likeOrCancelLike(Integer commentId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null){
            throw new BaseException("评论不存在");
        }
        Integer userId = UserHolder.getUserId();
        int count = commentMapper.getLikeCount(userId,commentId);
        // 没有点过赞，进行点赞操作，否则取消点赞
        if(count == 0){
            comment.setLikes(comment.getLikes() + 1);
            commentMapper.likeComment(userId,commentId);
        }else{
            comment.setLikes(comment.getLikes() - 1);
            commentMapper.cancelLikeComment(userId,commentId);
        }
        commentMapper.updateById(comment);
    }

    @Override
    public void deleteComment(Integer commentId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null){
            throw new BaseException("评论不存在");
        }
        Integer userId = UserHolder.getUserId();
        System.out.println("userId = " + userId);
        if (!userId.equals(comment.getUserId())){
            throw new BaseException("只能删除自己的评论");
        }
        commentMapper.deleteById(commentId);
    }
}




